#!/usr/local/bin/Resource/www/cgi-bin/php
<?php
function str_between($string, $start, $end){
	$string = " ".$string; $ini = strpos($string,$start);
	if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini;
	return substr($string,$ini,$len);
}

error_reporting(0);
exec ("rm -f /tmp/test.xml");
$link = $_GET["file"];
$link=urldecode($link);
$html=file_get_contents($link);
$l=str_between($html,'</iframe><iframe src="','"');
$h=file_get_contents($l);
$movie=str_between($h,"file': '","'");
$sub=str_between($h,"captions.file': '","'");
////////////////////////////////////////////////////////////////////////
$file=urldecode($sub);
$ttxml     = '';
$full_line = '';
$last_end=0;
if($file_array = file($file))
{
  //$ttxml .="<subtitrare>";
  foreach($file_array as $line)
  {
    $line = rtrim($line);
    $line = preg_replace("/(<\/?)(\w+)([^>]*>)/e","",$line);
        if(preg_match('/(\d\d):(\d\d):(\d\d),(\d\d\d) --> (\d\d):(\d\d):(\d\d),(\d\d\d)/', $line, $match))
        {
          $begin = round(3600 * $match[1] + 60 * $match[2] + $match[3] + $match[4]/1000);
          $end   = round(3600 *$match[5] + 60 * $match[6] + $match[7] + $match[8]/1000);
          //$begin = 3600 * $match[1] + 60 * $match[2] + $match[3] + round($match[4]/100)/10;
          //$end   = 3600 * $match[5] + 60 * $match[6] + $match[7] + round($match[8]/100)/10;
          $line1 = '';
          $line2 = '';
        $f = "/usr/local/bin/home_menu22";

        if (!file_exists($f)) {
          if ($begin > $last_end)
          {
   $ttxml .=$last_end."\n";
   $ttxml .=$begin."\n";
   $ttxml .="\n";
   $ttxml .="\n";
          }

          $last_end=$end;
        }
        }
        // if the next line is not blank, get the text
        elseif($line != '')
        {
          if($line1 != '')
          {
            $line2= $line;
          }
          else
          {
            $line1= $line;
          }
        }

        // if the next line is blank, write
        if($line == '')
        {
        if ($line2=="") {
$ttxml .=$begin."\n";
$ttxml .=$end."\n";
$ttxml .=$line2."\n";
$ttxml .=$line1."\n";
        } else {
$ttxml .=$begin."\n";
$ttxml .=$end."\n";
$ttxml .=$line1."\n";
$ttxml .=$line2."\n";
        }
          $line1 = '';
          $line2 = '';
        }
      }
//dummy sub
if ($end > 0) {
if (!file_exists($f))
   $ttxml .=$end."\n";
else
   $ttxml .="10000"."\n";
$ttxml .="10002"."\n";
$ttxml .="\n";
$ttxml .="\n";
}
$new_file = "/tmp/test.xml";
$fh = fopen($new_file, 'w');
fwrite($fh, $ttxml);
fclose($fh);
}
print $movie;
?>
